var request = require('request');
var co_request = require('co-request');
var moment = require('moment');
const appId = 'wxbc675391baa7ae28';
const appSecret = 'c9dc72fc2d2162fb7f8e65b97f1c3d5a';
const app_token = '12312312iuhihuwuheiuoqeiuqiuy';
const encoding_key = 'jp9QEREFdF8MC3qTgC1POpFhysR3N8bVyyz2zKdBxSI';
const tokenLib = require('./token');
const co = require('co');
module.exports = {

  //客服api
  //添加客服
  //get wechat access_token
  getAccessToken: function (call) {
    var apiUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appId+'&secret='+appSecret;
    request(apiUrl, function (err, resp, body) {
      if (err) return call(err);
      console.log(body);
      return call(JSON.parse(body).access_token);
    })
  },

  getAccessTokenSync: function *() {
    var apiUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appId+'&secret='+appSecret;
    let json = yield co_request(apiUrl);
    var token = JSON.parse(json.body).access_token;
    console.log('get token sync:',token);
    return token;
  },

  addClientService: function (json, call) {
    Token.find({type: 'access_token'}).then(function (record) {
      var accessToken = record[0].value;
      request({
        url: 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token='+accessToken,
        method: 'POST',
        json: true,
        headers: {
          'Content-Type': 'application/json'
        },
        body: json
      }, function (err, resp, body) {
        if (!err) return call(body);
      })
    })
  },

  //修改客服
  updateClientService: function (json, call) {
    Token.find({type: 'access_token'}).then(function (record) {
      var accessToken = record[0].value;
      request({
        url: 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token='+accessToken,
        method: 'POST',
        json: true,
        headers: {
          'Content-Type': 'application/json'
        },
        body: json
      }, function (err, resp, body) {
        if (!err) return call(body);
      })
    })
  },

  //del
  deleteClientService: function (json, call) {
    Token.find({type: 'access_token'}).then(function (record) {
      var accessToken = record[0].value;
      request({
        url: 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token='+accessToken,
        method: 'POST',
        json: true,
        headers: {
          'Content-Type': 'application/json'
        },
        body: json
      }, function (err, resp, body) {
        if (!err) return call(body);
      })
    })
  },

  //wechat js-sdk socket
  getJsTicket: function (call) {
    tokenLib.getWechatToken(function (token) {
      request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='+token+'&type=jsapi', function (err, resp, body) {
        if (err) return call(err);
        return call(JSON.parse(body).ticket);
      })
    });
  },

  get_token_by_code: function (code, call) {
    var get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+appId+'&secret='+appSecret+'&code='+code+'&grant_type=authorization_code';
    request(get_token_url, function (err, resp, body) {
      if (err) return call(err);
      return call(JSON.parse(body));
    })
  },

  get_info_by_token: function (access_token, openid, call) {
    var user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='+access_token+'&openid='+openid+'&lang=zh_CN';
    request(user_info_url, function (err, resp, body) {
      if (err) return call(err);
      return call(JSON.parse(body));
    })
  }
}
